package com.woniuxy.oldobject.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.oldobject.entity.Address;
import com.woniuxy.oldobject.entity.ResponseEntity;
import com.woniuxy.oldobject.entity.pageData;
import com.woniuxy.oldobject.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * description: AddressController <br>
 * 地址管理Controller
 * date: 2022/7/20 3:49 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */
@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     * 查询地址列表(分页+搜索条件)
     *
     * @param pageData
     * @return
     */
    @PostMapping("/list")
    public ResponseEntity findAllAddressPage(@RequestBody pageData pageData) {
        PageHelper.startPage(pageData.getPage(), pageData.getLimit());
        List<Address> addressPage = addressService.queryAll(pageData);
        PageInfo<Address> pageInfo = new PageInfo<>(addressPage);
        if (addressPage != null) {
            return new ResponseEntity(200, "查询成功", pageInfo);
        }
        return ResponseEntity.SUCCESS;
    }

    @RequestMapping("/query")
    //查询地址列表，不带条件
    public ResponseEntity queryaddres() {
        List<Address> addressList = addressService.list();
        if (addressList != null) {
            return new ResponseEntity(200, "查询地址列表成功", addressList);
        } else {
            return ResponseEntity.ERROR;
        }
    }

    /**
     * 新增地址
     *
     * @param address 地址对象
     * @return 是否新增成功
     */
    @PostMapping("/add")
    public ResponseEntity AddAddress(@RequestBody Address address) {
        address.setCreateTime(new Date());
        boolean flag = addressService.save(address);
        if (flag) {
            return new ResponseEntity(200, "新增成功", flag);
        }
        return ResponseEntity.SUCCESS;
    }

    /**
     * 新增和删除同一个方法(有ID就是更新，没ID就是新增)
     *
     * @param address
     * @return
     */
    @PostMapping("/saveAndUpdate")
    public ResponseEntity SaveAndUpdate(@RequestBody Address address) {
        boolean flag = false;
        if (address.getId() == null) {
            address.setCreateTime(new Date());
            address.setUpdateTime(new Date());
            flag = addressService.save(address);
            return new ResponseEntity(200, "新增成功", flag);
        } else {
            flag = addressService.saveOrUpdate(address);
            return new ResponseEntity(200, "跟新成功", flag);
        }
    }

    /**
     * 根据条件跟新地址表中的数据
     *
     * @param address 地址对象
     * @return 是否更新成功
     */
    @PutMapping("/update")
    public ResponseEntity UpdateAddress(@RequestBody Address address) {
        address.setUpdateTime(new Date());
        boolean flag = addressService.saveOrUpdate(address);
        if (flag) {
            return new ResponseEntity(200, "跟新成功", flag);
        }
        return ResponseEntity.SUCCESS;
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity DeleteAddress(@PathVariable("id") int id) {
        boolean flag = addressService.removeById(id);
        if (flag) {
            return new ResponseEntity(200, "删除成功", flag);
        }
        return ResponseEntity.SUCCESS;
    }

    /**
     * 根据题目id,批量删除地址
     *
     * @param pageData
     * @return
     */
    @DeleteMapping("/deleteAll")
    public ResponseEntity DeleteAllAddress(@RequestBody pageData pageData) {
        int i = addressService.deleteAllAddressByIds(pageData);
        if (i > 0) {
            return new ResponseEntity(200, "批量删除成功", i);
        }
        return ResponseEntity.SUCCESS;
    }
}
